//-----------------------------------------------------------------------------
// Name: Bullet.cpp
// Auth: Paul Godfrey
// Desc: Handles the creation, destruction and updating of a bullet
//-----------------------------------------------------------------------------

#include "Bullet.h"
//#include "DisplayManager.h"
#include <GL/glut.h>
#include <math.h>
#include "SharedConstants.h"
#include <iostream>
#include "Bounding.h"

static int bulletcount = 0;
cBullet gBullet;


//-----------------------------------------------------------------------------
// Functions:
//-----------------------------------------------------------------------------
cBullet* GetBullet()
{
	return &gBullet;
}
//Default constructor
cBullet::cBullet() :
	mDamage(1),
	mStatus(true),
	mSpeed(5),
	mNum(bulletcount)
{
	bulletcount++;

	mBounding.SetSphere(mPosition[0], mPosition[1], mPosition[2], 0.1f, kSphereBody);

}

//Constructor that takes in position, yaw and pitch
cBullet::cBullet(float x, float Yy, float z, float y, float p, int d, float l) :
	mYaw(y),
	mPitch(p),
	mDamage(d),
	mStatus(true),
	mSpeed(kBulletSpeed),
	mNum(bulletcount),
	mLifeSpan(l)
{
	bulletcount++;
	mPosition[0]=x;
	mPosition[1]=Yy;
	mPosition[2]=z;
	mDistance[0]=0;
	mDistance[1]=0;
	mDistance[2]=0;

	mBounding.SetSphere(mPosition[0], mPosition[1], mPosition[2], 0.1f, kSphereBody);
}

cBullet::~cBullet()
{
}

//Draws a sphere  at bullet position
void cBullet::Draw()
{
	glMatrixMode(GL_MODELVIEW);
	glPushMatrix();
	glDisable(GL_LIGHTING);
	glTranslatef(mPosition[0], mPosition[1], mPosition[2]);
	glPushMatrix();
	glTranslatef(0.0f,0.0f,0.0f);
	glColor3f(1.0f,0.0f,0.0f);
	glutSolidSphere(0.05f,10,10);
	glPopMatrix();
	glEnable(GL_LIGHTING);
	glPopMatrix();
}

//Sets a position
void cBullet::SetPosition(float x, float y, float z)
{
	mPosition[0]=x;
	mPosition[1]=y;
	mPosition[2]=z;
	mBounding.SetSphere(mPosition[0], mPosition[1], mPosition[2], 0.1f, kSphereBody);
}

//Sets the fired status (true=WILL be updated, false = not updated and will be deleted)
void cBullet::SetFired(bool f)
{
	mStatus=f;
}

//Returns fired status
bool cBullet::GetFired()
{
	return mStatus;
}

//Set bullet pitch
void cBullet::SetPitch(float p)
{
	mPitch=p;
}

//Set bullet yaw
void cBullet::SetYaw(float y)
{
	mYaw = y;
}

//Update the bullets position
void cBullet::Update(float elapsedTimeS)
{	
	float xFDirection = sin((mYaw)*kDegreesToRadians);
	float yVDirection = sin((mPitch*1.3f)*kDegreesToRadians);
	float zFDirection = cos((mYaw)*kDegreesToRadians);

	mPosition[0] += ((mSpeed * elapsedTimeS) * xFDirection);
	mPosition[1] += ((mSpeed * elapsedTimeS) * yVDirection);
	mPosition[2] += ((mSpeed * elapsedTimeS) * zFDirection);

	mDistance[0] += ((mSpeed * elapsedTimeS) * xFDirection);
	mDistance[1] += ((mSpeed * elapsedTimeS) * yVDirection);
	mDistance[2] += ((mSpeed * elapsedTimeS) * zFDirection);

	distance = sqrt((mDistance[0]*mDistance[0])+(mDistance[1]*mDistance[1])+(mDistance[2]*mDistance[2]));

	//If it gets to the end of its life without hitting anything - set fired to false (will be destroyed)
	if (distance>mLifeSpan || mPosition[1]<0.0f || mPosition[1]>20.0f)
	{
		mStatus=false;
	}

	mBounding.SetSphere(mPosition[0], mPosition[1], mPosition[2], 0.1f, kSphereBody);

}

//Get bullet pitch
float cBullet::GetPitch()
{
	return mPitch;
}

//Get bullet yaw
float cBullet::GetYaw()
{
	return mYaw;
}

//Get position x
float cBullet::GetPositionX()
{
	return mPosition[0];
}

//Get position Y
float cBullet::GetPositionY()
{
	return mPosition[1];
}

//Get position z
float cBullet::GetPositionZ()
{
	return mPosition[2];
}

//Get bounding for bullet
cBounding* cBullet::GetBounding()
{
	return &mBounding;
}

//Get damage of bullet
int cBullet::GetDamage()
{
	return mDamage;
}

//Set bullet status
void cBullet::SetStatus(bool s)
{
	mStatus=s;
}

//Get distance of bullet 
float cBullet::GetDist()
{
	return distance;
}

//Get bullet lifespawn
float cBullet::GetLifeSpan()
{
	return mLifeSpan;
}